// pages/order/orderTypeList/orderTypeList.js
const controller = require("../../controllers/Controller").controller
const controllerOrder = require('../../controllers/controllerOrder').controller
const controllerIndex = require("../../controllers/controllerIndex").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItem: 0,
    selectIndex: 0,
    lowLine: 0,
    types: "1,6,7",
    pageNumber: 1,
    genre: 1,
    accountId: "",
    amountType: 0,
    list: [],
    lastPage: false,
    lineOrderTypeList: ['到店自取', '直邮到家'],
    isMail: 0

  },

  //跳转线下详情页
  toDetail(e) {
    let id = e.currentTarget.dataset.id
    let no = e.currentTarget.dataset.no
    if (this.data.selectIndex == 0) {
      wx.navigateTo({
        url: '/pages/order/productOrder-detail/productOrder-detail?id=' + no,
      })
    }
    if (this.data.selectIndex == 1) {
      wx.navigateTo({
        url: '/pages/settlement/OrderDetails/OrderDetails?id=' + id,
      })
    }
    if (this.data.selectIndex == 2) {
      wx.navigateTo({
        url: '/pages/order/orderTypeList/VipCardDetail?id=' + no,
      })
    }
  },
  // 到店/直邮
  lineOrderChange(e) {
    let isMail = e.currentTarget.dataset.ismail
    this.setData({
      isMail
    })
    this.getOnLineList()

  },

  // 进行tab跳转选项
  selectTab(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
        pageNumber: 1,
        selectIndex: index
    })
    if (this.data.selectIndex == 0) {
      
      this.getOnLineList()
    } else if (this.data.selectIndex == 1) {
      this.getLowLine()
    } else {
      this.setData({
        lowLine: 1,
        types: "3"
      })
      this.getList()
    }
  },

  //获取线上商品订单
  getOnLineList(isFirst = true) {
    if (isFirst) {
      this.setData({
        list: [],
        pageNumber: 1,
        lastPage: false,
      })
    }
    if (this.data.lastPage) return
    
    let pageNumber = this.data.pageNumber
    let value = {
      type: 1,
      pageNumber,
      isMail: this.data.isMail,
      shopId: wx.getStorageSync('shopId'),
      accountId: this.data.accountId,
    }
    controllerOrder.userOrderList(value).then(res => {
      let list = this.data.list
      list = list.concat(res.data.list)
      pageNumber += 1
      this.setData({
        list,
        pageNumber,
        lastPage: res.data.lastPage
      })
    })
  },
  //获取开卡记录
  getList() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.genre = this.data.genre
    value.amountType = this.data.amountType
    value.lowLine = this.data.lowLine
    value.types = this.data.types
    value.shopId = wx.getStorageSync('shopId')
    value.accountId = this.data.accountId
    controller.getTransactionList(value).then(res => {
      this.setData({
        list: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  //获取线下开单列表
  getLowLine() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.shopId = wx.getStorageSync('shopId')
    value.accountId = this.data.accountId
    controllerIndex.selectOrder(value).then(res => {
      this.setData({
        list: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  loadMore() {
    if (this.data.lastPage == false) {
      if (this.data.selectIndex == 0) {
        let value = {}
        value.pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: value.pageNumber
        })
        value.genre = this.data.genre
        value.amountType = this.data.amountType
        value.lowLine = this.data.lowLine
        value.types = this.data.types
        value.shopId = wx.getStorageSync('shopId')
        value.accountId = this.data.accountId
        controller.getTransactionList(value).then(res => {
          let list = this.data.list
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            list: list3,
            lastPage: res.data.lastPage
          })
        })
      } else if (this.data.selectIndex == 1) {
        let value = {}
        value.pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: value.pageNumber
        })
        value.shopId = wx.getStorageSync('shopId')
        value.accountId = this.data.accountId
        controllerIndex.selectOrder(value).then(res => {
          let list = this.data.list
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            list: list3,
            lastPage: res.data.lastPage
          })
        })
      }
    }
  },


  onLoad: function (options) {
    this.setData({
      accountId: options.accountId
    })
    this.getOnLineList()
  },


  onReachBottom: function () {
    if (this.data.selectIndex === 0) {
      this.getOnLineList(false)
    } else {
      this.loadMore()
    }
  },


  //跳转订单列表页
  // jumpToOrderList(e){
  //   let type=e.currentTarget.dataset.type
  //   if(type==1){
  //     wx.navigateTo({
  //       url: '/pages/order/service-order/service-order?accountId='+this.data.accountId+"&id=0",
  //     })
  //   }else if(type==2){
  //     wx.navigateTo({
  //       url: '/pages/order/product-order/product-order?accountId='+this.data.accountId+"&id=0",
  //     })
  //   }else if(type==3){
  //     wx.navigateTo({
  //       url: '/pages/order/cloud-order/cloud-order?accountId='+this.data.accountId+"&id=0",
  //     })
  //   }
  // }
})